/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.emergency.plan.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.emergency.plan.dto.EmergencyParkPlanDTO;
import com.hopes.bizz.emergency.plan.entity.EmergencyParkPlanEntity;
import com.hopes.bizz.emergency.plan.entity.EpEmergencyPlanFileEntity;
import com.hopes.bizz.emergency.plan.entity.ErEmergencyPlanAuditRecordEntity;
import com.hopes.bizz.emergency.plan.mapper.EmergencyParkPlanMapper;
import com.hopes.bizz.emergency.plan.service.EmergencyParkPlanService;
import com.hopes.bizz.emergency.plan.service.EpEmergencyPlanFileService;
import com.hopes.bizz.emergency.plan.service.ErEmergencyPlanAuditRecordService;
import com.hopes.bizz.emergency.plan.service.ErEmergencyPlanService;
import com.hopes.boss.common.security.service.BossUser;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.hopes.bizz.emergency.common.constant.PlanDicConstant;
import com.hopes.boss.common.security.util.SecurityUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 园区应急预案
 *
 * @author cx
 * @date 2023-10-23 18:41:39
 */
@Service
public class EmergencyParkPlanServiceImpl extends ServiceImpl<EmergencyParkPlanMapper, EmergencyParkPlanEntity> implements EmergencyParkPlanService {

    @Resource
    private  ErEmergencyPlanAuditRecordService erEmergencyPlanAuditRecordService;

    @Resource
    private EpEmergencyPlanFileService epEmergencyPlanFileService;

    @Override
    public void handlerAudit(EmergencyParkPlanEntity emergencyParkPlan) {
        //选择的状态
        Integer preStatus = emergencyParkPlan.getStatus();
        UpdateWrapper<EmergencyParkPlanEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(EmergencyParkPlanEntity::getId,emergencyParkPlan.getId())
                .set(EmergencyParkPlanEntity::getStatus,preStatus)
                .set(EmergencyParkPlanEntity::getReleaseDate,LocalDate.now());

            if (PlanDicConstant.EMERGENCY_STATUS_WAIT_RELEASE.equals(preStatus)
                    ||PlanDicConstant.EMERGENCY_STATUS_AUDIT_FAILED.equals(preStatus)
                    ||PlanDicConstant.EMERGENCY_STATUS_ALREADY_RELEASE.equals(preStatus)){
                updateWrapper.lambda().set(EmergencyParkPlanEntity::getAuditOpinion,emergencyParkPlan.getAuditOpinion());
                ErEmergencyPlanAuditRecordEntity recordEntity = new ErEmergencyPlanAuditRecordEntity();
                recordEntity.setEmergencyPlanId(emergencyParkPlan.getId());
                recordEntity.setAuditStatus(preStatus);
                recordEntity.setAuditOpinion(emergencyParkPlan.getAuditOpinion());
                recordEntity.setAuditUser(SecurityUtils.getUser().getName());
                recordEntity.setCreateUserId(SecurityUtils.getUserId());
                erEmergencyPlanAuditRecordService.save(recordEntity);
            }
        this.update(updateWrapper);
    }

    @Override
    public Object savePark(EmergencyParkPlanDTO emergencyParkPlan) {
        EmergencyParkPlanEntity emergencyParkPlanEntity = new EmergencyParkPlanEntity();
        BeanUtils.copyProperties(emergencyParkPlan,emergencyParkPlanEntity);
        this.save(emergencyParkPlanEntity);
        List<EpEmergencyPlanFileEntity> fileList = emergencyParkPlan.getFileList();
        fileList.forEach(fileEntity -> fileEntity.setEmergencyPlanId(emergencyParkPlanEntity.getId()));
        return epEmergencyPlanFileService.saveFile(fileList);
    }

    @Override
    public Object updatePark(EmergencyParkPlanDTO emergencyParkPlan) {
        EmergencyParkPlanEntity emergencyParkPlanEntity = new EmergencyParkPlanEntity();
        BeanUtils.copyProperties(emergencyParkPlan,emergencyParkPlanEntity);
        this.updateById(emergencyParkPlanEntity);
        List<EpEmergencyPlanFileEntity> fileList = emergencyParkPlan.getFileList();
        fileList.forEach(fileEntity -> fileEntity.setEmergencyPlanId(emergencyParkPlanEntity.getId()));
        return epEmergencyPlanFileService.updateFile(fileList);
    }
}